package com.small.nacos.core.distributed.distro.task.delay;

import com.small.nacos.common.task.NacosTaskProcessor;
import com.small.nacos.common.task.engine.NacosDelayTaskExecuteEngine;
import com.small.nacos.core.distributed.distro.entity.DistroKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author zhoujin
 * @Date 2022/11/12 11:02
 */
public class DistroDelayTaskExecuteEngine extends NacosDelayTaskExecuteEngine {

    private static final Logger LOGGER = LoggerFactory.getLogger(DistroDelayTaskExecuteEngine.class);

    public DistroDelayTaskExecuteEngine() {
        super(DistroDelayTaskExecuteEngine.class.getName(),LOGGER);
    }

    @Override
    public void addProcessor(Object key, NacosTaskProcessor taskProcessor) {
        Object actualKey = getActualKey(key);
        super.addProcessor(actualKey, taskProcessor);
    }

    @Override
    public void removeProcessor(Object key) {
        Object actualKey = getActualKey(key);
        super.removeProcessor(actualKey);
    }

    @Override
    public NacosTaskProcessor getProcessor(Object key) {
        Object actualKey = getActualKey(key);
        return super.getProcessor(actualKey);
    }

    private Object getActualKey(Object key) {
        return key instanceof DistroKey ? ((DistroKey) key).getResourceType() : key;
    }
}
